﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ToDoList.Entity.Models;

namespace ToDoList.Logic
{
    public class ListMethod:IDisposable
    {
        private ToDoListDataContext _ctx = new ToDoListDataContext();
        /// <summary>
        /// 创建清单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool CreateList(int userId, string listName)
        {
            var user = _ctx.UsersData.SingleOrDefault(Id => Id.UserId == userId);
            if (user == null)
            {
                return false;
            }
            else
            {
                var list = new ListData()
                {
                    UserId = userId,
                    ListName = listName,
                    CreationTime = DateTime.Now
                };
                _ctx.ListData.Add(list);
                _ctx.SaveChanges();
                return true;
            }

        }
        /// <summary>
        /// 浏览清单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<ListData> BrowseList(int userId)
        {
            var lists = _ctx.ListData.Where(Id => Id.UserId == userId).ToList();
            if (lists != null)
            {
                return lists;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 添加事件
        /// </summary>
        /// <param name="lsitId"></param>
        /// <param name="OverdueTime"></param>
        /// <param name="ItemsContent"></param>
        /// <returns></returns>
        public bool AddItems(int userId, int listId, string OverdueTime, string ItemsContent)
        {
            var user = _ctx.ListData.SingleOrDefault(Id => Id.UserId == userId);
            var list = _ctx.ListData.SingleOrDefault(d => d.ListId == listId);
            if (user == null && list == null)
            {
                return false;
            }
            else
            {
                var item = new ItemsData()
                {
                    UserId = userId,
                    ListId = listId,
                    CreationTime = DateTime.Now,
                    OverdueEvent = DateTime.Parse(OverdueTime),
                    State = false,
                    EvenName = ItemsContent
                };
                _ctx.ItemsData.Add(item);
                _ctx.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="listId"></param>
        /// <param name="itemsId"></param>
        /// <returns></returns>
        public bool DeleteItems(int userId, int listId, int itemsId)
        {
            var list = _ctx.ListData.SingleOrDefault(Id => Id.ListId == listId);
            var matter = _ctx.ItemsData.SingleOrDefault(Id => Id.EventId == itemsId);
            if (list == null && matter == null)
            {
                return false;
            }
            else
            {
                var item = _ctx.ItemsData.Find(itemsId);
                _ctx.ItemsData.Remove(item);
                _ctx.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 修改清单名字
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="listId"></param>
        /// <param name="newListName"></param>
        /// <returns></returns>
        public bool ModifyListName(int userId, int listId, string newListName)
        {
            var listid = _ctx.ListData.SingleOrDefault(Id => Id.ListId == listId);
            var user = _ctx.UsersData.SingleOrDefault(name => name.UserId == userId);
            if (listid == null && user == null)
            {
                return false;
            }
            else
            {
                var list = _ctx.ListData.Find(listId);
                list.ListName = newListName;
                _ctx.SaveChanges();
                return true;
            }
        }
        public void Dispose()
        {
            _ctx.Dispose();
        }
    }
}
